/**
 * Shows the forum page
 */
function showForum(bool) {
	if(bool == 1){
		$(".ajaxSitePage").hide();
		$("#forumpage").fadeIn();
		$("html, body").animate({ scrollTop: 0 }, "slow");
	}else
		$('#myModal').modal('show');
}

/**
 * Shows the new topic page
 */
function showNewTopic() {
	$(".ajaxSitePage").hide();
	$("#forumNewTopic").fadeIn();
	$("html, body").animate({ scrollTop: 0 }, "slow");
}

/**
 * Shows tags from a search
 */
function findTags(path, elem) {
	var search = elem.value;
	if(search.length > 1){
		$.ajax({
	        type: "POST",
	        url : path,
	        data : "search="+search,
	        beforeSend: function(){
	        	$("#loading").show();
	        },
	        success: function(data){
	        	$("#tag_results").html(data);
	        	$("#loading").hide();
	        }
	    });
	}
}

function addToTags(elem){
	var oldVal = $("#tags_selected").val();
	$("#tags_selected").val($("#"+elem).text() + " " +oldVal);
	$("#tag_search").val('');
	$("#tag_results").html('');
}

function createTopic(){
	event.preventDefault();
	
	var elem = $("#topic_new");
        $.ajax({
            type: "POST",
            url : elem.attr("action"),
            data: elem.serialize(),
            success: function(data){
            	elem.each(function(){
            		this.reset();

            	});
            	showForum(); 
            }
        });
}

function showMyTopics(path){
	 $.ajax({
            type: "POST",
            url : path,
            success: function(data){
            	$(".ajaxSitePage").hide();
            	$("#topicpage").html(data);
				$("#topicpage").fadeIn();
				$("html, body").animate({ scrollTop: 0 }, "slow"); 
            }
        });
	
}

function editTopic(path){
	 $.ajax({
            type: "POST",
            url : path,
            success: function(data){
            	$(".ajaxSitePage").hide();
            	$("#editpage").html(data);
				$("#editpage").fadeIn();
				$("html, body").animate({ scrollTop: 0 }, "slow"); 
            }
        });
	
}

function updateTopic(){
	event.preventDefault();
	
	var elem = $("#topic_edit");
        $.ajax({
            type: "POST",
            url : elem.attr("action"),
            data: elem.serialize(),
            success: function(data){
            	elem.each(function(){
            		$(".ajaxSitePage").hide();
            		$("#topicpage").html(data);
					$("#topicpage").fadeIn();
					$("html, body").animate({ scrollTop: 0 }, "slow"); 

            	});
            }
        });
}